package dimension.starry.gui.Component;

import com.mojang.blaze3d.systems.RenderSystem;
import dimension.starry.StarryDimension;
import dimension.starry.constants.PlayerDataType;
import dimension.starry.dataManager.NetWorkRegister;
import dimension.starry.dataManager.PlayerDataManager;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

@Environment(EnvType.CLIENT)
public class AddPointWidget extends ButtonWidget {

    private static final Text stringTextComponent = Text.empty();
    private static final Identifier BACKGROUND_RESOURCE = new Identifier(
        StarryDimension.MOD_ID, "textures/gui/player_info/add_point_widget.png");

    private final Text labelText1;
    private final Text labelText2;
    private final PlayerDataType playerDataType;

    public AddPointWidget(int left, int top, PlayerDataType playerDataType) {
        super(
            left, top, 15, 41, stringTextComponent,
            (Button) -> NetWorkRegister.sendAddAttributePacket(playerDataType.getName()), supplier -> Text.empty()
        );
        this.playerDataType = playerDataType;
        this.labelText1 = Text.translatable(
            StarryDimension.MOD_ID + ".gui.player_info.label." + playerDataType.getName() + 1);
        this.labelText2 = Text.translatable(
            StarryDimension.MOD_ID + ".gui.player_info.label." + playerDataType.getName() + 2);
    }

    @Override
    public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) {
        drawContext.drawTexture(BACKGROUND_RESOURCE, this.getX(), this.getY(), 0, 0, width, height, width, height);
        TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;

        drawContext.drawTextWithShadow(
            textRenderer, String.valueOf(PlayerDataManager.getInt(null, playerDataType)), this.getX() + 7,
            this.getY() + 23, 16777215
        );
        drawContext.drawTextWithShadow(textRenderer, labelText1, this.getX() + 4, this.getY() + 2, 16777215);
        drawContext.drawTextWithShadow(textRenderer, labelText2, this.getX() + 4, this.getY() + 12, 16777215);
    }

}
